﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CNFM_CROCODILE_LOGO.Core.Domain.ViewModels;
using CNFM_CROCODILE_LOGO.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace CNFM_CROCODILE_LOGO_Web.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    public class RoleController : Controller
    {
        private IBasicServices _pro;
        public RoleController(IBasicServices pro)
        {
            this._pro = pro;
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <returns></returns>
        public IActionResult List()
        {
            return View();
        }
        /// <summary>
        /// 角色修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult Edit(string id)
        {
            var result = _pro.GetRole(id);
            var json = JsonConvert.SerializeObject(result.Result);
            JObject obj = JObject.Parse(json);
            RoleModels entity = JsonConvert.DeserializeObject<RoleModels>(obj["Data"].ToString());

            return View(entity);
        }
        /// <summary>
        /// 角色新增
        /// </summary>
        /// <returns></returns>
        public IActionResult Add()
        {
            return View();
        }


        /// <summary>
        /// 菜单授权
        /// </summary>
        /// <returns></returns>
        public IActionResult MeunEdit(string id)
        {
            ViewData["Id"] = id;
            return View();
        }

        /// <summary>
        /// 角色菜单
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public JsonResult GetRoleMenu(string roleid)
        {
            var ret = _pro.GetRoleMenu(roleid);
            return Json(ret);
        }

        /// <summary>
        /// 角色授权菜单
        /// </summary>
        /// <param name="paramete"></param>
        /// <returns></returns>
        public JsonResult AddUpdateRoleMenu(string paramete)
        {
            UpdateRoleMenu entity = JsonConvert.DeserializeObject<UpdateRoleMenu>(paramete);
            var ret = _pro.AddUpdateRoleMenu(entity);
            return Json(ret);
        }

        /// <summary>
        /// 角色列表
        /// </summary>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <param name="sasearch"></param>
        /// <returns></returns>
        public JsonResult GetRoleAll(int index, int size, string sasearch)
        {
            var ret = _pro.GetRoleAll(index, size, sasearch);
            return Json(ret);
        }

        /// <summary>
        /// 角色新增修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddUpdateRole([FromBody]RoleModels model)
        {
            var ret = _pro.AddUpdateRole(model);
            return Json(ret);
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult DelectRole(string roleId)
        {
            var ret = _pro.DelectRole(roleId);
            return Json(ret);
        }

        /// <summary>
        /// 角色选择下拉框
        /// </summary>
        /// <returns></returns>
        public JsonResult GetRoleSelect()
        {
            var ret = _pro.GetRoleSelect();
            return Json(ret);
        }
    }
}